import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/userpage',
    children: [{
      path: 'userpage',
      name: 'Userpage',
      component: () => import('@/views/userpage/index'),
      meta: { title: '管理员首页', icon: 'userpage' }
    }]
  },

  {
    path: '/de_manage',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'De_manage',
        component: () => import('@/views/de_manage/index'),
        meta: { title: '设备基础信息管理', icon: 'de_manage' }
      }
    ]
  },

  {
    path: '/fault',
    component: Layout,
    redirect: '/fault/fault_report',
    name: 'Fault',
    meta: { title: '故障诊断', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/fault_report/index'),
        meta: { title: '故障填报', icon: 'table' }
      },
      {
        path: 'pie',
        name: 'Pie',
        component: () => import('@/views/fault_analysis/index'),
        meta: { title: '故障分析', icon: 'pie' }
      }
    ]
  },

  {
    path: '/de_repair',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'De_repair',
        component: () => import('@/views/de_repair/index'),
        meta: { title: '设备维修', icon: 'de_repair' }
      }
    ]
  },

  {
    path: '/search',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Search',
        component: () => import('@/views/search/index'),
        meta: { title: '综合查询', icon: 'search' }
      }
    ]
  },

  {
    path: '/pre_repair',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Pre_repair',
        component: () => import('@/views/pre_repair/index'),
        meta: { title: '预防性检修', icon: 'pre_repair' }
      }
    ]
  },
  {
    path: '/main_manage',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'main_manage',
        component: () => import('@/views/task_overview/index'),
        meta: { title: '任务概览', icon: 'weixiurenyuan' }
      }
    ]
  }
  // {
  //   path: '/test',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       name: 'Test',
  //       component: () => import('@/views/test/index'),
  //       meta: { title: '测试用页', icon: '' }
  //     }
  //   ]
  // }
]

export const asyncRoutes = [
  {
    path: '/sp_manage',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Sp_manage',
        component: () => import('@/views/sp_manage/index'),
        meta: {
          title: '备件基础资料管理',
          icon: 'sp_manage',
          roles: ['SystemAdmin', 'PartAdmin']
        }
      }
    ]
  },
  {
    path: '/sys_setting',
    component: Layout,
    redirect: '/sys_setting/role_manage',
    name: 'sys_setting',
    meta: {
      title: '系统设置',
      icon: 'sys_setting',
      roles: ['SystemAdmin']
    },
    children: [
      {
        path: 'user_manage',
        component: () => import('@/views/sys_setting/user_manage'), // Parent router-view
        name: 'user_manage',
        meta: { title: '人员管理', icon: 'renyuan' }
      },
      {
        path: 'role_manage',
        component: () => import('@/views/sys_setting/role_manage'),
        name: 'role_manage',
        meta: { title: '权限管理', icon: 'quanxian' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
